package cn.edu.njuit.children.service.impl;

import cn.edu.njuit.children.entity.Children;
import cn.edu.njuit.children.entity.model.HeightModel;
import cn.edu.njuit.children.service.ChildrenService;
import cn.edu.njuit.children.utils.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.edu.njuit.children.entity.Height;
import cn.edu.njuit.children.service.HeightService;
import cn.edu.njuit.children.mapper.HeightMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author Administrator
* @description 针对表【sys_height】的数据库操作Service实现
* @createDate 2022-11-29 08:58:00
*/
@Service
public class HeightServiceImpl extends ServiceImpl<HeightMapper, Height>
    implements HeightService{

    @Autowired
    HeightMapper heightMapper;

    @Autowired
    ChildrenService childrenService;

    @Override
    public boolean addHeight(HeightModel o) throws Exception {
        Children byId = childrenService.getById(o.getCdId());
        if (byId==null){
            throw new Exception("未找到幼儿信息");
        }
        Height height = new Height();
        if (o.getRecordTime()!=null){
            Date date = new Date(o.getRecordTime());
            height.setRecordTime(date);

            Long birthDay = byId.getCdBirthday().getTime();
            height.setRecordDay(DateUtil.getDatePoor(birthDay,o.getRecordTime()));
        }
        height.setCdHeight(o.getCdHeight());
        height.setCdAge(o.getCdAge());
        height.setCdId(o.getCdId());

        return this.save(height);
    }

    @Override
    public boolean upHeight(HeightModel o) throws Exception {
        Height height = heightMapper.selectById(o.getId());
        if (height==null){
            throw new Exception("未找到身高信息");
        }
        Children children = new Children();
        if (o.getCdId()!=null){
            children = childrenService.getById(o.getCdId());
            if (children==null){
                throw new Exception("未找到幼儿信息");
            }
            height.setCdId(o.getCdId());
        }else {
            children = childrenService.getById(height.getCdId());
        }
        if (o.getRecordTime()!=null){
            Date date = new Date(o.getRecordTime());
            height.setRecordTime(date);

            Long birthDay = children.getCdBirthday().getTime();
            height.setRecordDay(DateUtil.getDatePoor(birthDay,o.getRecordTime()));
        }
        height.setCdHeight(o.getCdHeight());
        height.setCdAge(o.getCdAge());

        return this.updateById(height);
    }
}




